from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from accounts.models import User, UserPorfile, UserAddress


# Register your models here.


@admin.register(User)
class UserAdmin(UserAdmin):
    '''用户基础信息'''
    list_display = ('format_username', 'nickname', 'integral', 'is_active')
    # 按用户名昵称进行搜索
    search_fields = ('username', 'nickname')
    # 添加自定义方法
    actions = ['disable_user', 'enable_user']
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (('Personal info'), {'fields': ('first_name', 'last_name', 'email',
                                        'integral', 'nickname', 'avatar')}),
        (('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )


    def format_username(self, obj):
        '''用户名脱敏处理'''
        return obj.username[0:3] + '****'
    # 修改列名显示
    format_username.short_description = '用户名'


    def disable_user(self, request, queryset):
        '''自定义方法 批量禁用选中用户'''
        queryset.update(is_active=False)
    disable_user.short_description = '批量禁用用户'

    def enable_user(self, request, queryset):
        '''批量启用用户'''
        queryset.update(is_active=True)
    enable_user.short_description = '批量启用用户'
# admin.site.register(User, UserAdmin)


@admin.register(UserPorfile)
class UserPorfileAdmin(admin.ModelAdmin):
    '''用户详细信息管理'''

    list_display = ('user', 'phone_no', 'sex')


@admin.register(UserAddress)
class UserAddressAdmin(admin.ModelAdmin):
    '''用户地址管理'''
    list_display = ('user', 'province', 'city', 'username',
                    'address', 'phone', 'is_valid', 'is_default')

    search_fields = ('user__username', 'user__nickname', 'phone', 'username')
